package crm.view.main;

import crm.view.Custom.CheckCustomSpending;
import crm.view.Custom.LoginBasicCustom;
import crm.view.Custom.LookBasicCustom;
import crm.view.Custom.commitFeedBack;
import crm.view.LoginPage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalTime;

public class CustomerMain implements ActionListener, Runnable {
    JFrame frame = null;
    JMenuBar menuBar = null;
    JMenuItem manageMenu = null;
    JMenuItem checkMenu = null;
    JMenuItem lockMenu = null;
    JPanel panel = null;
    JMenuItem leave = null;
    JMenu basic = null;
    JMenu others = null;
    JMenu feedback = null;
    JMenuItem feedbackMenu = null;
    JLabel label = null;

    JMenu time = null;
    JMenuItem stop = null;
    JMenuItem over = null;

    JTextField text;
    Timer time2;

    public static void main(String[] args) {
        CustomerMain customerMain = new CustomerMain();
        new Thread(customerMain).start();
    }

    public CustomerMain() {
        init();
        frame.setBounds(680, 300, 1096, 680);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.setJMenuBar(menuBar);
        frame.setVisible(true);
    }

    public void init() {
        frame = new JFrame("welcome,用户您好！");
        panel = new JPanel();
        menuBar = new JMenuBar();
        menuBar.setBounds(0, 0, 30, 100);
        manageMenu = new JMenuItem("注册基本信息");
        checkMenu = new JMenuItem("查看基本信息");
        lockMenu = new JMenuItem("查看消费情况");
        basic = new JMenu("用户功能");
        others = new JMenu("其他功能");
        leave = new JMenuItem("退出");
        label = new JLabel("用户管理页面");
        label.setFont(new Font("楷体", Font.BOLD, 40));
        label.setBounds(450, 20, 300, 60);
        frame.add(label);

        feedback = new JMenu("反馈信息");
        feedbackMenu = new JMenuItem("提交反馈");


        time2 = new Timer(1000, this); //WindowTime对象做计时器的监视器
        text = new JTextField(10);
        text.setBounds(800, 50, 200, 50);
        text.setEditable(false);
        frame.add(text);

        time = new JMenu("计时功能");
        stop = new JMenuItem("暂停");
        over = new JMenuItem("继续");

        time.add(stop);
        time.addSeparator();
        time.add(over);

        Font font1 = new Font("宋体", Font.BOLD, 30);
        basic.setFont(font1);
        others.setFont(font1);
        time.setFont(font1);

        ImageIcon icon1 = new ImageIcon("src/img/消费.png");
        lockMenu.setIcon(icon1);
        ImageIcon icon2 = new ImageIcon("src/img/注册 (1).png");
        manageMenu.setIcon(icon2);
        ImageIcon icon3 = new ImageIcon("src/img/个人信息.png");
        checkMenu.setIcon(icon3);
        ImageIcon icon4 = new ImageIcon("src/img/退出.png");
        leave.setIcon(icon4);
        ImageIcon icon5 = new ImageIcon("src/img/计时.png");
        time.setIcon(icon5);
        ImageIcon icon6 = new ImageIcon("src/img/其他.png");
        others.setIcon(icon6);
        ImageIcon icon8 = new ImageIcon("src/img/暂停.png");
        stop.setIcon(icon8);
        ImageIcon icon9 = new ImageIcon("src/img/结束.png");
        over.setIcon(icon9);
        ImageIcon icon10 = new ImageIcon("src/img/用户.png");
        basic.setIcon(icon10);
        ImageIcon icon11 = new ImageIcon("src/img/反馈.png");
        feedback.setIcon(icon11);
        feedbackMenu.setIcon(icon11);


        menuBar.add(basic);
        basic.add(manageMenu);
        basic.addSeparator();       // 添加一条分割线
        basic.add(checkMenu);
        basic.addSeparator();       // 添加一条分割线
        basic.add(lockMenu);

        menuBar.add(time);
        menuBar.add(others);
        others.add(leave);

        menuBar.add(feedback);
        feedback.add(feedbackMenu);

        ImageIcon icon = new ImageIcon("src/img/顾客.jpg");
        JLabel imgLabel = new JLabel(icon);
        imgLabel.setBounds(0, -200, icon.getIconWidth(), icon.getIconHeight());
        panel.add(imgLabel);
        frame.add(panel);


        Font font2 = new Font("宋体", Font.BOLD, 25);
        manageMenu.setFont(font2);
        checkMenu.setFont(font2);
        lockMenu.setFont(font2);
        leave.setFont(font2);

        stop.setFont(font2);
        over.setFont(font2);
        text.setFont(font2);

        feedback.setFont(font2);
        feedbackMenu.setFont(font2);

        stop.addActionListener(this);
        over.addActionListener(this);

        manageMenu.addActionListener(this);
        checkMenu.addActionListener(this);
        lockMenu.addActionListener(this);
        leave.addActionListener(this);
        feedbackMenu.addActionListener(this);
    }

    @Override
    public void run() {
        time2.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == time2) {
            LocalTime dateTime = LocalTime.now();
            String timeStr =
                    dateTime.getHour() + ":" + dateTime.getMinute() + ":" + dateTime.getSecond();
            text.setText(timeStr);
        } else if ("注册基本信息".equals(e.getActionCommand()))
            new LoginBasicCustom();
        else if ("查看基本信息".equals(e.getActionCommand()))
            new LookBasicCustom();
        else if ("查看消费情况".equals(e.getActionCommand()))
            new CheckCustomSpending();
        else if ("退出".equals(e.getActionCommand())) {
            new LoginPage();
            frame.dispose();
        } else if ("暂停".equals(e.getActionCommand()))
            time2.stop();
        else if ("继续".equals(e.getActionCommand()))
            time2.restart();
        else if ("提交反馈".equals(e.getActionCommand())){
            new commitFeedBack();
        }


    }


}
